<template>
  <!-- 能耗分析树形控件 -->
  <Row style="height:100%;">
    <Tree
      v-if="baseDataList.length>0"
      :data="baseDataList"
      :show-checkbox="showCheckbox"
      :check-strictly="associated"
      @on-select-change="selectChange"
    ></Tree>
    <img
      v-if="baseDataList.length==0"
      src="~@/assets/img/efficiencyAnalysis/zwsj.png"
      width="50%"
      style="display: block; margin: auto; position:relative; top: 30%;"
    />
  </Row>
</template>

<script>
import {
  custEnergySystemList,
  custEnergyDeptList,
  custEnergyAreaList,
  custEnergyLineList
} from "@/api/efficiency.js";
import util from "@/libs/util.js";
export default {
  props: {
    showCheckbox: {
      type: Boolean,
      default: false
    },
    checkNode: {
      type: Function,
      default: null
    },
    associated: {
      type: Boolean,
      default: true
    },
    apiType: {
      type: String,
      default: "01"
    },
  },
  data() {
    return {
      selectedFlag: false,
      baseDataList: [],
      title: "",
      checkList: [],
      checkState: false,
      token: null,
      checkDouble: [],
      selectOne: [],
    };
  },
  computed: {},
  watch: {
    title(val) {
      custEnergySystemList({
        cliqueCustNo: val,
        custId: this.$route.query.custId || util.token.initToken().custId,
        cliqueId: this.$route.query.cliqueId || util.token.initToken().cliqueId,
        type: "1"
      }).then(res => {
        if (res.code == 200) {
          let baseData = this.filterNode([res.result], 0);
          this.baseDataList = baseData;
         
        } else {
          this.baseDataList = [];
        }
      });
    }
  },
  methods: {
    selectChange(nodes, node) {
      this.selectedFlag = true
      let arr = []
      if(this.selectedFlag) {
        this.baseDataList = JSON.parse(JSON.stringify(this.baseDataList))
        this.selectedFlag = false
      }
      this.checkNode(nodes, node);
    },
    filterNode(list) {
      list.map(res => {
        res.title = res.name;
        if (
          "children" in res &&
          res.children != null &&
          res.children.length > 0
        ) {
          this.filterNode(res.children);
        }
      });
      return list;
    }
  },
  created() {
    this.token = util.token.initToken();
    if (this.apiType == "01") {
      custEnergySystemList({
        custId: this.$route.query.custId || util.token.initToken().custId,
        type: "1"
      }).then(res => {
        if (res.code == 200) {
          let baseData = this.filterNode(res.result);
          this.baseDataList = baseData;
          this.baseDataList[0].selected = true;
          this.$emit('getnodeId',this.baseDataList[0])
        } else {
          this.baseDataList = [];
        }
      });
    } else if (this.apiType == "02") {
      custEnergyDeptList({
        custId: this.$route.query.custId || util.token.initToken().custId,
        type: "1"
      }).then(res => {
        if (res.code == 200) {
          let baseData = this.filterNode(res.result);
          this.baseDataList = baseData;
          this.baseDataList[0].selected = true;
          this.$emit('getnodeId',this.baseDataList[0])
        } else {
          this.baseDataList = [];
        }
      });
    } else if (this.apiType == "03") {
      custEnergyAreaList({
        custId: this.$route.query.custId || util.token.initToken().custId,
        type: "1"
      }).then(res => {
        if (res.code == 200) {
          let baseData = this.filterNode(res.result);
          this.baseDataList = baseData;
          this.baseDataList[0].selected = true;
          this.$emit('getnodeId',this.baseDataList[0])
        } else {
          this.baseDataList = [];
        }
      });
    } else if (this.apiType == "04") {
      custEnergyLineList({
        custId: this.$route.query.custId || util.token.initToken().custId,
        type: "1"
      }).then(res => {
        if (res.code == 200) {
          let baseData = this.filterNode(res.result);
          this.baseDataList = baseData;
          this.baseDataList[0].selected = true;
          this.$emit('getnodeId',this.baseDataList[0])
        } else {
          this.baseDataList = [];
        }
      });
    }
  },
  mounted() {},
  beforeCreate() {}, // 生命周期 - 创建之前
  beforeMount() {}, // 生命周期 - 挂载之前
  beforeUpdate() {}, // 生命周期 - 更新之前
  updated() {}, // 生命周期 - 更新之后
  beforeDestroy() {}, // 生命周期 - 销毁之前
  destroyed() {}, // 生命周期 - 销毁完成
  activated() {} // 如果页面有keep-alive缓存功能，这个函数会触发
};
</script>